package cn.com.dhcc.turbo.service.exch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.HttpClientUtil;
import cn.com.dhcc.app.core.util.Status;
import cn.com.dhcc.app.core.util.XstreamUtil;
import cn.com.dhcc.turbo.entity.api.Data;
import cn.com.dhcc.turbo.entity.api.Exch;
import cn.com.dhcc.turbo.entity.api.FileItem;
import cn.com.dhcc.turbo.entity.api.PlatformArg;
import cn.com.dhcc.turbo.entity.api.Posthead;


/**
 * http 调用服务
 * @author Administrator
 *
 */
public class HttpDataExchRecvService {
	@SuppressWarnings("deprecation")
	private Logger logger = Log.getDetachLogger(LOGTYPE.TURBO);
	private final static String PARAM_NAME="exch";
	private String url;
	
	public String getUrl() {
		return url;
	}


	public void setUrl(String url) {
		this.url = url;
	}

	

	public HttpDataExchRecvService() {
		super();
	}


	public HttpDataExchRecvService(String url) {
		super();
		this.url = url;
	}


	
	/**
	 * 结构化通知接收者服务
	 * @param posthead 文件头
	 * @param metadata  数据描述信息
	 * @param data   数据信息
	 * @return
	 * @throws ServiceException 
	 */
	public String receive(String posthead, String metadata,String data) throws ServiceException{
		Exch exch=new Exch();
		exch.setActionName("receive");
		PlatformArg arg=new PlatformArg();
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			arg.setPostHead(XstreamUtil.toObject(Posthead.class, posthead));
			Data content=new Data();
			content.setMetadata(metadata);
			content.setData(data);
			exch.setPlatformArg(arg);
			exch.setContent(content);
			paramMap.put(HttpDataExchRecvService.PARAM_NAME,XstreamUtil.toXml(exch));
		} catch (Exception e) {
			logger.error(e);
			throw new ServiceException("转换成xml时出现异常");
		}
		
		Status status=HttpClientUtil.post(url, paramMap);
		if (status.isSuccess()) {
			return status.getData().toString();
		}else{
			throw new ServiceException(status.getMsg());
		}
	}
	
	
	
	/**
	 * 结构化通知发送者服务
	 * @param grougId
	 * @param orderId
	 * @param status
	 * @param receiveMsg
	 * @return
	 * @throws ServiceException 
	 */
	public String receiveNotify(String groupId, String orderId, String status, String receiveMsg,String metadata) throws ServiceException{
		Exch exch=new Exch();
		exch.setActionName("receiveNotify");
		PlatformArg arg=new PlatformArg();
		arg.setGroupId(groupId);
		arg.setOrderId(orderId);
		arg.setStatus(status);
		exch.setPlatformArg(arg);
		Data content=new Data();
		content.setMetadata(metadata);
		exch.setContent(content);
		exch.setReceiveMsg(receiveMsg);
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			paramMap.put(HttpDataExchRecvService.PARAM_NAME,XstreamUtil.toXml(exch));
		} catch (Exception e) {
			logger.error(e);
			throw new ServiceException("转换成xml时出现异常");
		}
		Status status1=HttpClientUtil.post(url, paramMap);
		if (status1.isSuccess()) {
			return status1.getData().toString();
		}else{
			throw new ServiceException(status1.getMsg());
		}
	}
	

	/**
	 * 非结构化
	 * @param posthead
	 * @param metadata
	 * @param fileItems
	 * @return
	 * @throws ServiceException 
	 */
	public String receiveFileMsg(String posthead, String metadata,List<String> fileItems) throws ServiceException{
		Exch exch=new Exch();
		exch.setActionName("receiveFileMsg");
		PlatformArg arg=new PlatformArg();
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			arg.setPostHead(XstreamUtil.toObject(Posthead.class, posthead));
			Data content=new Data();
			content.setMetadata(metadata);
			content.setFileItems(this.toFileItems(fileItems));
			exch.setPlatformArg(arg);
			exch.setContent(content);
			paramMap.put(HttpDataExchRecvService.PARAM_NAME,XstreamUtil.toXml(exch));
		} catch (Exception e) {
			logger.error(e);
			throw new ServiceException("转换成xml时出现异常");
		}
		Status status=HttpClientUtil.post(url, paramMap);
		if (status.isSuccess()) {
			return status.getData().toString();
		}else{
			throw new ServiceException(status.getMsg());
		}
	}
	
	
	private List<FileItem> toFileItems(List<String> fileItems) throws Exception{
		List<FileItem> list=new ArrayList<FileItem>();
		for (String string : fileItems) {
			list.add(XstreamUtil.toObject(FileItem.class, string));
		}
		return list;
	}
	
	/**
	 * 文件送达情况通知(这个方法需要数据发送方实现)
	 * @param grougId
	 * @param orderId
	 * @param status
	 * @param receiveMsg
	 * @return
	 * @throws ServiceException 
	 */
	public String receiveFileNotify(String groupId, String orderId, String status, String receiveMsg,String metadata) throws ServiceException{
		Exch exch=new Exch();
		exch.setActionName("receiveFileNotify");
		PlatformArg arg=new PlatformArg();
		arg.setGroupId(groupId);
		arg.setOrderId(orderId);
		arg.setStatus(status);
		exch.setPlatformArg(arg);
		Data content=new Data();
		content.setMetadata(metadata);
		exch.setContent(content);
		
		exch.setReceiveMsg(receiveMsg);
		HashMap<String, String> paramMap=new HashMap<String, String>();
		try {
			paramMap.put(HttpDataExchRecvService.PARAM_NAME,XstreamUtil.toXml(exch));
		} catch (Exception e) {
			logger.error(e);
			throw new ServiceException("转换成xml时出现异常");
		}
		Status status1=HttpClientUtil.post(url, paramMap);
		if (status1.isSuccess()) {
			return status1.getData().toString();
		}else{
			throw new ServiceException(status1.getMsg());
		}
	}
	
//	public static void main(String[] args) throws Exception {
//		HttpDataExchRecvService service=new HttpDataExchRecvService();
//		//service.receive("<posthead><groupid>123</groupid></posthead>", "123", "0");
//		
//		//service.receiveNotify("123", "123", "0","接收成功");
//		List<String> list=new ArrayList<String>();
//		
//	String str=	"<fileItem>"+
//		 "           <pathInfo>"+
//		"                <absolutePath>123</absolutePath>"+
//		 "               <relativePath>d:</relativePath>"+
//		 "           </pathInfo>"+
//		 "           <customFileMsg>ggggggggggggg</customFileMsg>"+
//		"        </fileItem>";
//		String str1= "       <fileItem>"+
//		 "           <pathInfo>"+
//		"                <absolutePath>345</absolutePath>"+
//		 "               <relativePath>e:</relativePath>"+
//		 "           </pathInfo>"+
//		 "           <customFileMsg>tttttttttt</customFileMsg>"+
//		"        </fileItem>";
//
//		/*list.add(str);
//		list.add(str1);
//		//service.receiveFileMsg("<posthead><groupid>123</groupid></posthead>", "123", list);
//		
//		service.receiveFileNotify("123", "123", "0","接收成功");
//		
//		
//		String exch="";
//		HashMap<String, String> paramMap=new HashMap<String, String>();
//		System.out.println(XstreamUtil.toXml(exch));
//		paramMap.put(HttpDataExchRecvService.PARAM_NAME,XstreamUtil.toXml(exch));
//		System.out.println(HttpClientUtil.post("", paramMap));;*/
//		
//		String mwr_cjw="<exch>"+
//	  "<actionName>send</actionName>"+
//	  "<platformArg>"+
//	    "<postHead>"+
//	  "    <from>"+
//	      "  <nodecode>010000201</nodecode>"+
//	     "   <adaptorcode>000000000001</adaptorcode>"+
//	     "   <auditor></auditor>"+
//	     " </from>"+
//	    " <to>"+
//	    "   <nodecode>010000201</nodecode>"+
//	    "   <sendTime></sendTime>"+
//	     " </to></postHead>"+
//	  " <authCode>3BB0F11C4AE6708C5591634A13EC282B</authCode>"+
//	  "  </platformArg>"+
//	 "  <content>"+
//	  "   <metadata></metadata>"+
//	    " <data>test</data></content>"+
//	 "</exch>";
//			final HashMap<String, String> mwr_cjwMap=new HashMap<String, String>();
//			mwr_cjwMap.put("exch",mwr_cjw);
//		//从中央到长委
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				for (int i = 1; i < 1001; i++) {
//					System.out.println("从中央到长委,第"+i+"次执行*************start*****************");
//					System.out.println(HttpClientUtil.post("http://10.1.201.7:8080/turbo/api/exch.do", mwr_cjwMap));
//					System.out.println("从中央到长委,第"+i+"次执行*************end*****************");
//				}
//			}
//		}).start();
			
		
		
//		String mwr_slw="<exch>"+
//				  "<actionName>send</actionName>"+
//				  "<platformArg>"+
//				    "<postHead>"+
//				  "    <from>"+
//				      "  <nodecode>010000201</nodecode>"+
//				     "   <adaptorcode>000000000001</adaptorcode>"+
//				     "   <auditor></auditor>"+
//				     " </from>"+
//				    " <to>"+
//				    "   <nodecode>010000201</nodecode>"+
//				    "   <sendTime></sendTime>"+
//				     " </to></postHead>"+
//				  " <authCode>3BB0F11C4AE6708C5591634A13EC282B</authCode>"+
//				  "  </platformArg>"+
//				 "  <content>"+
//				  "   <metadata></metadata>"+
//				    " <data>test</data></content>"+
//				 "</exch>";
//						final HashMap<String, String> mwr_slwMap=new HashMap<String, String>();
//						mwr_slwMap.put("exch",mwr_slw);
//			
//		//从中央到松辽
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
//				for (int i = 1; i < 1001; i++) {
//					System.out.println("从中央到长委,第"+i+"次执行*************start*****************");
//					System.out.println(HttpClientUtil.post("http://10.1.201.7:8080/turbo/api/exch.do", mwr_slwMap));
//					System.out.println("从中央到长委,第"+i+"次执行*************end*****************");
//				}
//			}
//		}).start();
							
		
//}
}
